package com.action.app;

import com.bean.ResponseBean;
import com.pojo.Apk;
import com.service.ApkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("app/v1/upgrade")
public class AppVersionManagerAction {

    @Autowired
    private ApkService apkService ;

    @Value("${host}")
    private String server ;

    /**
     * 检测版本升级
     * @param versionCode
     * @return
     */
    @RequestMapping("apkUpdate")
    public ResponseBean apkUpdate(String versionCode){
        String apkName = "VPN";
        Apk apk = apkService.getCurrentVersion(apkName);
        if (null != apk ) {
            if (Float.valueOf(apk.getVersions()) > Float.valueOf(versionCode)){
                String url = "upload" + apk.getTarget_url();
                Map map = new HashMap();
                map.put("version", apk.getVersions());
                map.put("url", server + url+ "?md5=" + apk.getMd5());
                return new ResponseBean(200, "success", map);
            }
        }
        return new ResponseBean(500, "不要升级", null);
    }

}
